$(function(){
    init();
});

var financialFormula = {
    PMT: function(ir, np, pv, fv, type ) {
        // console.log(ir, np, pv, fv, type);
        if( ir == 0 ) return pv / np;

        var pmt;
        if( type ){
            pmt = ( ir * ( pv * Math.pow ( (ir+1), np ) + fv ) ) / ( ( ir + 1 ) * ( Math.pow ( (ir+1), np) -1 ) );
        }else{
            pmt = ((pv - fv) * ir / (1 - Math.pow((1 + ir), -np))) + fv * ir;
        }
        return pmt;
    }

};

var models = {
    'all' : {
        'c51':{title:'smart fortwo 硬顶版 66 千瓦极致版',price:'176,000',model:'fortwo'},
        'c50':{title:'smart fortwo 硬顶版 66 千瓦先锋版',price:'156,000',model:'fortwo'},
        'c49':{title:'smart fortwo 硬顶版 52 千瓦激情版',price:'136,000',model:'fortwo'},
        'c48':{title:'smart fortwo 硬顶版 52 千瓦灵动版',price:'125,000',model:'fortwo'},
        'c61':{title:'smart fortwo 灰行侠特别版',price:'139,888',model:'fortwo'},
        'c62':{title:'smart fortwo 挚爱特别版',price:'140,888',model:'fortwo'},
        'c64':{title:'全新 smart fortwo 硬顶 BRABUS Xclusive',price:'228,000',model:'fortwo'},
        'c66':{title:'smart 新声特别版',price:'145,888',model:'fortwo'},
        'c67':{title:'smart fortwo Coupe 52KW sapphire blue ed',price:'145,888',model:'fortwo'},
        'c68':{title:'smart fortwo Coupe 66KW sapphire blue ed',price:'173,888',model:'fortwo'},
        'c56':{title:'smart forfour 4 门 4 座车 66 千瓦极致版',price:'186,000',model:'forfour'},
        'c55':{title:'smart forfour 4 门 4 座车 66 千瓦先锋版',price:'166,000',model:'forfour'},
        'c54':{title:'smart forfour 4 门 4 座车 52 千瓦激情版',price:'146,000',model:'forfour'},
        'c53':{title:'smart forfour 4 门 4 座车 52 千瓦灵动版',price:'135,000',model:'forfour'},
        'c63':{title:'smart forfour 4 门 4 座车挚爱特别版',price:'159,888',model:'forfour'},
        'c65':{title:'全新 smart forfour BRABUS Xclusive 4 门 4 座车',price:'238,000',model:'forfour'},
        'c69':{title:'smart forfour 4门4座吴亦凡特别版',price:'175,888',model:'forfour'},
        'c60':{title:'smart fortwo 敞篷版 66 千瓦极致版',price:'206,000',model:'cabrio'},
        'c59':{title:'smart fortwo 敞篷版 66 千瓦先锋版',price:'186,000',model:'cabrio'},
        'c58':{title:'smart fortwo 敞篷版 52 千瓦激情版',price:'166,000',model:'cabrio'}
    },
    'fortwo' : {
        'c51':{title:'smart fortwo 硬顶版 66 千瓦极致版',price:'176,000'},
        'c50':{title:'smart fortwo 硬顶版 66 千瓦先锋版',price:'156,000'},
        'c49':{title:'smart fortwo 硬顶版 52 千瓦激情版',price:'136,000'},
        'c48':{title:'smart fortwo 硬顶版 52 千瓦灵动版',price:'125,000'},
        'c61':{title:'smart fortwo 灰行侠特别版',price:'139,888'},
        'c62':{title:'smart fortwo 挚爱特别版',price:'140,888'},
        'c64':{title:'全新 smart fortwo 硬顶 BRABUS Xclusive',price:'228,000'},
        'c66':{title:'smart 新声特别版',price:'145,888'},
        'c67':{title:'smart fortwo Coupe 52KW sapphire blue ed',price:'145,888'},
        'c68':{title:'smart fortwo Coupe 66KW sapphire blue ed',price:'173,888'}
    },
    'forfour' : {
        'c56':{title:'smart forfour 4 门 4 座车 66 千瓦极致版',price:'186,000'},
        'c55':{title:'smart forfour 4 门 4 座车 66 千瓦先锋版',price:'166,000'},
        'c54':{title:'smart forfour 4 门 4 座车 52 千瓦激情版',price:'146,000'},
        'c53':{title:'smart forfour 4 门 4 座车 52 千瓦灵动版',price:'135,000'},
        'c63':{title:'smart forfour 4 门 4 座车挚爱特别版',price:'159,888'},
        'c65':{title:'全新 smart forfour BRABUS Xclusive 4 门 4 座车',price:'238,000'},
        'c69':{title:'smart forfour 4门4座吴亦凡特别版',price:'175,888'}
    },
    'cabrio' : {
        'c60':{title:'smart fortwo 敞篷版 66 千瓦极致版',price:'206,000'},
        'c59':{title:'smart fortwo 敞篷版 66 千瓦先锋版',price:'186,000'},
        'c58':{title:'smart fortwo 敞篷版 52 千瓦激情版',price:'166,000'}
    }
};

var dkinfo = {
    'fortwo' : {
        'tx0yf': {
            title:'零预付款先享后选弹性购车新方案',
            desc:'0 预付先享后选-弹性购车新方案，月供低至 2,799，24/36 个月还款，期末购买、返还、置换灵活选。'
        },
        'jr0yg': {
            title:'零利率金融贷款方案',
            desc:'0 利率金融贷款方案，20% 起超低首付，低月付，24/36 个月灵活还款。'
        },
        'jd30sf': {
            title:'99元 超低月供金融贷款方案',
            desc:'99元 起超低月供金融贷款方案，30% 低首付，24 个月轻松还款。'
        }
    },
    'forfour' : {
        'jr0yg':{
            title:'零利率金融贷款方案',
            desc:'0 利率金融贷款方案，20% 起超低首付，低月付，24/36 个月灵活还款。'
        },
        'tx0yf':{
            title:'零预付款先享后选弹性购车新方案',
            desc:'0 预付先享后选-弹性购车新方案，月供低至 2,999，24/36 个月还款，期末购买、返还、置换灵活选。'
        },
        'jd30sf':{
            title:'99元 超低月供金融贷款方案',
            desc:'99元 起超低月供金融贷款方案，30% 低首付，24 个月轻松还款。'
        }
    },
    'cabrio' : {
        'tx0yf':{
            title:'零预付款先享后选弹性购车新方案',
            desc:'0 预付先享后选-弹性购车新方案，月供低至 3,899，24/36 个月还款，期末购买、返还、置换灵活选。'
        },
        // 'tx50sf':{
        //     title:'smart星时享先享后选-弹性购车新方案',
        //     desc:'smart星时享“先享后选-弹性购车”新方案，50% 首付，极简手续，特快审批，超低月供，期末购买、返还、置换灵活选。'
        // },
        // 'jr0wk':{
        //     title:'For smart快速审批金融贷款方案',
        //     desc:'smart金融贷款方案，极简手续，光速审批，乐享快进式提车。'
        // },
        'jr0yg':{
            title:'零利率金融贷款方案',
            desc:'0 利率金融贷款方案，20% 起超低首付，低月付，24/36 个月灵活还款。'
        },
        'jd30sf':{
            title:'122元 超低月供金融贷款方案',
            desc:'122元 起超低月供金融贷款方案，30% 低首付，24 个月轻松还款。'
        }
    }
};


$('.car_choose img').click(function(){
    $(this).addClass('curr');
    var c = $(this).attr('src');
    var idx = $(this).index();
    for( var i=0; i<$('.car_choose img').length; i++ ){
        if( i==idx ) continue;
        var o = $('.car_choose img').eq(i).attr('src');
        $('.car_choose img').eq(i).attr('src', o.replace('_o', '')).removeClass('curr');
    }
    if( c.indexOf('_o') == -1 ){
        $(this).attr( 'src', c.replace('.png', '_o.png') );
    }
    var car = $('.car_choose .curr').attr('ref');

    var html = '';
    for( i in models[car] ){
        html += '<option value="'+ i.replace('c', '') +'">'+models[car][i].title+'</option>'
    }
    $('#s_model').html( html );

    var curType = $('#s_type').val();
    var options = '';
    for( var id in dkinfo[car] ){
        var dk = dkinfo[car][id];
        options += '<option value="'+id+'">'+dk.title+'</option>';
    }
    $('#s_type').html( options );
    $('#s_type').val( curType ? curType : 'jr0yg' );
    $('#s_type').trigger("change");
});



function init(){
    var car = $('.car_choose .curr').attr('ref');

    var html = '';
    for( i in models['all'] ){
        html += '<option value="'+ i.replace('c', '') +'" data-model="'+ models['all'][i].model +'">'+models['all'][i].title+'</option>'
    }
    $('#s_model').html( html );

    var curType = $('#s_type').val();
    var options = '';
    for( var id in dkinfo[car] ){
        var dk = dkinfo[car][id];
        options += '<option value="'+id+'">'+dk.title+'</option>';
    }
    $('#s_type').html( options );
    $('#s_type').val( curType ? curType : 'jr0yg' );
    $('#s_type').trigger("change");
}

function getCurrentCar( i ){
    var car = $('.car_choose .curr').attr('ref');
    return {model:car, idx:i, info:models[car]['c'+i]}
}

//方案选择
$('#s_type').change(function(){
    
    var p= $('#s_type').val();

    dataLayer.push({'event':'event', 'cat':'金融计算器_主视觉', 'act':'计算贷款', 'lbl':'购车方案'+$('#s_type').find("option:selected").text()+'_201508040003'});
    var car = $('.car_choose .curr').attr('ref');

    $('#fainfo').html(dkinfo[car][p].desc);
    $('.calculator').hide();
    if(p != -1){
        $('#'+p).show();$('#'+p).css("visibility","visible");$('#'+p).css("height","100%");
    }
    // $('#s_model').val(3);
    //车型事件
    $('#s_model').unbind().change(function(){
        var i = $('select#s_model').val();
        var model = $("select#s_model").find("option:selected").data('model');
        $('.car_choose .curr').attr('ref',model);
        console.log(i);
        var car = getCurrentCar( i );

        var m = $('#s_type').val();
        realmoney = 0;
        if(m == 'tx0yf'){
            var p= $('#s_model').val();

            var bzj = money2f( car.info.price ) * tx0yf[p].bzj / 100;
            $('#promise_money').html(fmoney(Math.round(bzj), 2));

            //初始化 状态
            
            $('select#lc').val(10000);
            $('select#lc').trigger("click");
            $('select#lc').trigger("change");
            huantu();
        }else if(m == 'tx50sf'){
            var p= $('#s_model').val();
            $('select#lc50').val(10000);
            $('select#lc50').trigger("click");
            $('select#lc50').trigger("change");
            $('#s_limitMonth50').val(24);
            $('#s_limitMonth50').trigger("change");
            huantu();
        }else if(m == 'jr0wk'){
            var p= $('#s_model').val();
            $('#s_limitMonth2').val(24);
            $('#s_limitMonth2').trigger("change");
            huantu();
        }else if(m == 'jr0yg'){
            var p= $('#s_model').val();
            //$('#s_limitMonth3').val(24);
            $("#s_limitMonth3").find("li").eq(0).trigger("click");
            huantu();
        }else if(m == 'jd30sf'){
            var p= $('#s_model').val();
            var id = $('#s_model').val();

            $('#show_money4').html(fmoney(car.info.price));
            $('input[name=real_money4]').val(fmoney(car.info.price));
            $('#yfk4').html(fmoney(money2f( car.info.price ) * jd30sf[id].rate[0] / 100));
            $('#12yfk').html(fmoney(money2f( car.info.price ) * jd30sf[id].rate[1] / 100));
            $('#zgyfk').html(fmoney(money2f( car.info.price ) * jd30sf[id].rate[2] / 100));
            //console.log(jd30sf[id].monthly);
            $('#text-handle_yf4').html(fmoney(jd30sf[id].monthly));
            huantu();
        }
        $('.ui-slider-tooltip .ttContent').html( '10,000' );
        dataLayer.push({'event':'event', 'cat':'金融计算器_主视觉', 'act':'计算贷款', 'lbl':'车型'+$('#s_model').find("option:selected").text()+'_201508040004'});
    });

    $('#s_model').trigger("change");
});

function huantu(){
    var i = $('select#s_model').val();
    var car = getCurrentCar( i );

    $('.cartext').html(car.info.title);

//    $('.finance-car').css('background-image','url('+CHANNEL_IMAGE_PATH + '/bg_'+car.model+'.png)');
//    $('.finance-car-img img').attr('src',CHANNEL_IMAGE_PATH + '/' + 'c' + car.idx + '.png');
    $('.yfje').html(car.info.price);
}

function fmoney(s, n) {
    n = n==undefined||n<0||n>=20?0:n;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
    //console.log(s, n);
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("");
}
function money2f( m ){
    return parseFloat( m.replace(',','') );
}
